import requests

from lxml import etree


class RequestTools:
    def __init__(self):
        self._headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
        }

    # 返回html文本
    def get_html(self, url, encoding = 'utf-8', method = 'get', data=None):
        if method == 'get':
            response = requests.get(url, headers = self._headers)
        else:
            response = requests.post(url, headers = self._headers, data = data)
        response.encoding = encoding
        return response.text

    def get_html_tree(self, html_str):
        return etree.HTML(html_str)

    # request_post请求
    def request_post(self, url, data=None, headers = None, method='post', res='json'):
        global response
        ua = 'User-Agent'
        if ua not in headers:
            headers[ua] = self._headers[ua]
        if method == 'post':
            response = requests.post(url, headers = headers, data = data)
        elif method == 'get':
            response = requests.get(url, headers = headers, params = data)
        if res == 'json':
            return response.json()
        elif res == 'text':
            return response.text
if __name__ == '__main__':
    rt = RequestTools()
    html = rt.get_html('https://pic.netbian.com/', encoding = 'gbk')
    print(html)
